package com.rainsoftware.LicenseSystem.controllers.api;

import com.rainsoftware.LicenseSystem.services.ActivationService;
import com.rainsoftware.LicenseSystem.utils.StandardResponse;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/activation")
public class ActivationController {

    @Autowired
    private ActivationService activationService;

    @PostMapping("/activate")
    public ResponseEntity<String> activate(
            @RequestParam(required = false) String license_key,
            @RequestParam(required = false) String product_sdk_key,
            @RequestParam(required = false) String machine,
            @RequestBody(required = false) String jsonBody
    ) {
        if (jsonBody != null) {
            JSONObject json = new JSONObject(jsonBody);
            license_key = json.optString("license_key");
            product_sdk_key = json.optString("product_sdk_key");
            machine = json.optString("machine");
        }
        StandardResponse response = activationService.activateLicense(license_key, product_sdk_key, machine);
        return response.build();
    }
}